package com.yxh.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.yxh.common.annotation.Authorization;
import com.yxh.pojo.FineType;
import com.yxh.pojo.User;
import com.yxh.service.FineTypeService;
import com.yxh.service.UserService;
import com.yxh.util.Response;
import com.yxh.util.SnowflakeIdWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static com.yxh.util.APIErrorCode.SUCCESS;

/**
 * Created by 32988 on 2018/6/2.
 */
@Controller
@RequestMapping("/type/")
public class FineTypeController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(FineTypeController.class);
    private Response response;

    @Autowired
    private FineTypeService fineTypeService;

    /**
     * 保存
     *
     * @param fineType
     * @return
     */
    @PostMapping("save_fine_type")
    @ResponseBody
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    public Response saveFineType(@RequestBody FineType fineType) {
        fineType.setFineTypeId(SnowflakeIdWorker.creatId());
        fineTypeService.save(fineType);
        response = Response.returnResponse(SUCCESS);
        response.setData(fineType.getFineTypeId());
        return response;
    }


    /**
     * 查询
     *
     * @param fineType
     * @return
     */
    @GetMapping("get_information")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response getInformation(FineType fineType, String page, String pageSize) {
        Integer limit;
        Integer offset;
        try {
            limit = Integer.parseInt(page);
            offset = Integer.parseInt(pageSize);
        } catch (Exception e) {
            limit = 1;
            offset = 10;
        }
        PageInfo pageInfo = fineTypeService.getInformation(fineType, limit, offset);
        response = Response.returnResponse(SUCCESS);
        response.setData(pageInfo);

        return response;
    }

    /**
     * 修改
     *
     * @param fineType
     * @return
     */
    @RequestMapping(value = "update_fineTypeInfo", method = RequestMethod.PUT)
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response updateFineTypeInfo(@RequestBody FineType fineType) {
        fineTypeService.update(fineType);
        response = Response.returnResponse(SUCCESS);
        return response;
    }

    /**
     * 删除
     *
     * @return
     */
    @DeleteMapping("/{id}")
    @Authorization
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "authorization", required = true, dataType = "string", paramType = "header"),
    })
    @ResponseBody
    public Response deleteFineType(@PathVariable("id") String id) {
        fineTypeService.delete(id);
        response = Response.returnResponse(SUCCESS);
        return response;
    }

}
